package com.example.fw;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public abstract class WebDriverHelperBase extends HelperBase {

	protected WebDriver driver;

	public WebDriverHelperBase(ApplicationManager manager) {
		super(manager);
		this.driver = manager.getDriver();
	}

	protected WebElement findElement(By linkText) {
		try {
			return driver.findElement(linkText);
		} catch (Exception e) {
			return null;
		}
	}

	public boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	public void openUrl(String location) {
		driver.get(manager.getProperty("baseURL")
				+ manager.getProperty("firstPage") + location);
	}

	public void openAbsoluteUrl(String location) {
		driver.get(location);
	}

	protected void type(By locator, String text) {
		if (text != null) {
			driver.findElement(locator).clear();
			driver.findElement(locator).sendKeys(text);
		}
	}

	protected void click(By locator) {
		driver.findElement(locator).click();
	}

	protected void selectByText(By locator, String text) {
		if (text != null) {
			new org.openqa.selenium.support.ui.Select(
					driver.findElement(locator)).selectByVisibleText(text);
		}
	}
}
